package com.sinothk.hussars.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sinothk.hussars.entity.*;
import com.sinothk.hussars.mapper.WorkerMapper;
import com.sinothk.hussars.service.*;
import com.sinothk.hussars.utils.XUtils;
import com.sinothk.server.support.annotation.callback.BizException;
import com.sinothk.server.support.author.TokenUtil;
import com.sinothk.server.support.entity.ResultCode;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Service("loginService")
public class LoginServiceImpl implements LoginService {

    private final WorkerManagerService workerManagerService;
    private final WorkerCheckerService workerCheckerService;
    private final WorkerService workerService;
    private final UserService userService;

    public LoginServiceImpl(WorkerManagerService workerManagerService, WorkerCheckerService workerCheckerService, WorkerService workerService, UserService userService) {
        this.workerManagerService = workerManagerService;
        this.workerCheckerService = workerCheckerService;
        this.workerService = workerService;
        this.userService = userService;
    }

    @Resource
    private WorkerMapper workerMapper;

    @Override
    public LoginWorkerDTO loginForWorker(String orgType, String account, String password) {

        if("01".equals(orgType)){// 监管部门》卫健局工作人员
            LambdaQueryWrapper<WorkerManagerEntity> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(WorkerManagerEntity::getAccount, account);
            WorkerManagerEntity dbWorker = workerManagerService.getOne(queryWrapper);
            if (dbWorker == null) throw new BizException(ResultCode.USER_NOT_EXIST);
            // 验证密码
            if (!password.equals(dbWorker.getPassword())) throw new BizException(ResultCode.USER_LOGIN_ERROR);

            // 配置相关参数
            LoginWorkerDTO workerDto = XUtils.classUtil().copyByMethod(dbWorker, WorkerManagerEntity.class, LoginWorkerDTO.class);
            workerDto.setToken(getTokenVal(dbWorker.getAccount(), dbWorker.getOrgCode(), dbWorker.getDeptCode(), dbWorker.getJobCode(), dbWorker.getPermissions()));
            return workerDto;
        }

        if("02".equals(orgType)){ // 检测机构
            LambdaQueryWrapper<WorkerCheckerEntity> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(WorkerCheckerEntity::getAccount, account);
            WorkerCheckerEntity dbWorker = workerCheckerService.getOne(queryWrapper);
            if (dbWorker == null) throw new BizException(ResultCode.USER_NOT_EXIST);
            // 验证密码
            if (!password.equals(dbWorker.getPassword())) throw new BizException(ResultCode.USER_LOGIN_ERROR);

            // 配置相关参数
            LoginWorkerDTO workerDto = XUtils.classUtil().copyByMethod(dbWorker, WorkerCheckerEntity.class, LoginWorkerDTO.class);
            workerDto.setToken(getTokenVal(dbWorker.getAccount(), dbWorker.getOrgCode(), dbWorker.getDeptCode(), dbWorker.getJobCode(), dbWorker.getPermissions()));
            return workerDto;
        }

        if("03".equals(orgType)){ // 采集机构Web端
            LambdaQueryWrapper<WorkerEntity> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(WorkerEntity::getAccount, account);
            WorkerEntity dbWorker = workerService.getOne(queryWrapper);
            if (dbWorker == null) throw new BizException(ResultCode.USER_NOT_EXIST);
            // 验证密码
            if (!password.equals(dbWorker.getPassword())) throw new BizException(ResultCode.USER_LOGIN_ERROR);

            // 配置相关参数
            LoginWorkerDTO workerDto = XUtils.classUtil().copyByMethod(dbWorker, WorkerEntity.class, LoginWorkerDTO.class);
            workerDto.setToken(getTokenVal(dbWorker.getAccount(), dbWorker.getOrgCode(), dbWorker.getDeptCode(), dbWorker.getJobCode(), dbWorker.getPermissions()));
            return workerDto;
        }

        throw new BizException("机构类型有误");
    }

    private String getTokenVal(String account, String orgCode, String deptCode, String jobCode, String permissions) {
        Map<String, Object> claimsInfo2 = new HashMap<>();
        claimsInfo2.put("account", account);
        claimsInfo2.put("orgCode", orgCode);
        claimsInfo2.put("deptCode", deptCode);
        claimsInfo2.put("jobCode", jobCode);
        claimsInfo2.put("permissions", permissions);
        return TokenUtil.createToken(claimsInfo2, TokenUtil.TIME_DAY_30);
    }

    @Override
    public LoginVo login(String idNum, String password) {
        LambdaQueryWrapper<UserEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserEntity::getIdNum, idNum);
        UserEntity dbUser = userService.getOne(queryWrapper);
        if (dbUser == null) throw new BizException(ResultCode.USER_NOT_EXIST);
        // 验证密码
        if (!password.equals(dbUser.getPassword())) throw new BizException(ResultCode.USER_LOGIN_ERROR);
        // 配置相关参数
        LoginVo workerDto = XUtils.classUtil().copyByMethod(dbUser, UserEntity.class, LoginVo.class);
        Map<String, Object> claimsInfo = new HashMap<>();
        claimsInfo.put("idNum", dbUser.getIdNum());
        workerDto.setToken(TokenUtil.createToken(claimsInfo, TokenUtil.TIME_DAY_30));
        return workerDto;
    }
}
